// KetonalSettings.cpp: implementation of the KetonalSettings class.
//
//////////////////////////////////////////////////////////////////////

#include "KetonalSettings.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

KetonalSettings::KetonalSettings() : thread ("ketonal settings config cache thread")
{
	ketonalProperties = 0;
	ready = false;
	directoryList = 0;

	File f (PropertiesFile::getDefaultAppSettingsFile (T("ketonal"), T("settings"), String::empty, false));

	if (!f.existsAsFile())
	{
		f.create();
	}

	ketonalProperties = new PropertiesFile (f, 0, PropertiesFile::storeAsXML);

	defaultConfigsDirectory		= ketonalProperties->getValue (T("defaultConfigsDirectory"), File::getSpecialLocation (File::userApplicationDataDirectory).getFullPathName() + T("\\ketonal\\configs"));
	if (!readAndCacheConfigs())
	{
		ready = false;
	}

	defaultRectangle		= ketonalProperties->getValue (T("defaultRectangle"), T("10 10 130 130"));
}

KetonalSettings::~KetonalSettings()
{
	Logger::writeToLog (T("settings dtor()"));
	save();

	if (ketonalProperties)
		deleteAndZero (ketonalProperties);

	deleteAndZero (directoryList);
}

void KetonalSettings::save()
{
	ketonalProperties->setValue (T("defaultRectangle"), defaultRectangle);
	ketonalProperties->setValue (T("defaultConfigsDirectory"), defaultConfigsDirectory);
	ketonalProperties->save();
}

String KetonalSettings::getDefaultConfigsDirectory()
{
	return (defaultConfigsDirectory);
}

void KetonalSettings::setDefaultConfigsDirectory(String t)
{
	defaultConfigsDirectory = t;
}

bool KetonalSettings::readAndCacheConfigs()
{
	Logger::writeToLog (T("readConfigs and Cache()"));
	const File f(defaultConfigsDirectory.toUTF8());

	if (f.exists() && f.isDirectory())
	{	
		thread.startThread ();
		
		directoryList = new DirectoryContentsList (0, thread);
		
		if (!directoryList)
		{
			return (false);
		}

		directoryList->setDirectory (defaultConfigsDirectory, false, true);
		directoryList->refresh();
		
		return (true);
	}
	else if (!f.isDirectory())
	{
		if (f.createDirectory())
		{
			thread.startThread ();
		
			directoryList = new DirectoryContentsList (0, thread);
		
			if (!directoryList)
			{
				return (false);
			}

			directoryList->setDirectory (defaultConfigsDirectory, false, true);
			directoryList->refresh();

			return (true);
		}
		else
		{
			return (false);
		}
	}
	else if (!f.exists())
	{
		if (f.createDirectory())
		{
			thread.startThread ();
		
			directoryList = new DirectoryContentsList (0, thread);
		
			if (!directoryList)
			{
				return (false);
			}

			directoryList->setDirectory (defaultConfigsDirectory, false, true);
			directoryList->refresh();

			return (true);
		}
		else
		{
			return (false);
		}
	}
	else
	{
		return (false);
	}
}

bool KetonalSettings::painFree()
{
	if (directoryList)
		return (directoryList->isStillLoading());
	else
		return (false);
}

Rectangle KetonalSettings::getRect ()
{
	return (Rectangle::fromString (defaultRectangle));
}

void KetonalSettings::setRect (Rectangle rect)
{
	defaultRectangle = rect.toString();
}

int KetonalSettings::getConfigCount()
{
	return (directoryList->getNumFiles());
}

DirectoryContentsList *KetonalSettings::getContentsList()
{
	if (directoryList)
	{
		return (directoryList);
	}
	else
	{
		return (0);
	}
}